<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="formData"
        ref="formRef"
        :inline="true"
        label-width="110px"
        v-loading="formLoading"
      >
        <el-form-item label="订单号" prop="orderNumber">
          <el-input
            v-model="formData.orderNumber"
            placeholder="请输入订单号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-140px"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery" type="primary">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        ref="multipleTableRef"
        border
        row-key="orderNumber"
        :height="tableHeight"
        v-loading="loading"
        :data="list"
        @select-all="selectAll"
        @select="select"
        @selection-change="handleSelectionChange"
        selection-type="none"
      >
        <el-table-column align="center" type="selection" />
        <el-table-column label="序号" align="center" type="index" fixed="left" width="80" />
        <el-table-column label="订单号" prop="orderNumber" />
        <el-table-column label="客户代码" prop="customerCode" />
        <el-table-column label="客户名称" prop="customerName" />
        <el-table-column label="订单类型" prop="dictOrderType">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.MARKET_ORDER_TYPE" :value="scope.row.dictOrderType" />
          </template>
        </el-table-column>
        <el-table-column label="产品编号" prop="productNumber" />
        <el-table-column label="产品名称" prop="productName" />
        <el-table-column label="规格型号" prop="modelSpecification" />
        <el-table-column label="单位" prop="partUnit" />
        <el-table-column label="订单数量" prop="orderQuantity" />
        <el-table-column label="状态" prop="dictTaskBookStatus">
          <template #default="scope">
            <dict-tag
              :type="DICT_TYPE.ORDER_TASK_BOOK_STATUS"
              :value="scope.row.dictTaskBookStatus"
            />
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="formData.pageNo"
        v-model:limit="formData.pageSize"
        @pagination="getCustomerProfileList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
  import { DICT_TYPE } from '@/utils/dict'
  import * as ProductionPrepareApi from '@/api/produce/productManagement/productionPrepare'
  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const total = ref(0) // 列表的总页数
  const multipleTableRef = ref()
  const orderNumber = ref()
  const formData = ref({
    orderNumber: undefined,
    pageNo: 1,
    pageSize: 10,
    dictTaskBookStatus: 20
  })
  const list = ref<any>([]) // 列表的数据

  const formRef = ref() // 表单 Ref

  /** 打开弹窗 */
  const open = async (data) => {
    
    orderNumber.value = data
    dialogVisible.value = true
    getCustomerProfileList()
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    let params = {
      list: selectList.value
    }
    // 校验表单
    emit('success', params)
    dialogVisible.value = false
  }
  //重置按钮
  const resetQuery = () => {
    formData.value.orderNumber = undefined
    formData.value.pageNo = 1
    formData.value.pageSize = 10
    handleQuery()
  }
  /** 搜索按钮操作 */
  const handleQuery = () => {
    getCustomerProfileList()
  }
  const loading = ref(true) // 列表的加载中
  //获取客户档案列表
  const getCustomerProfileList = async () => {
    loading.value = true
    try {
      const data = await ProductionPrepareApi.getOrderInfo(formData.value)
      list.value = data.list
      total.value = data.total
      nextTick(() => {
        for (let i = 0; i < list.value.length; i++) {
          if (list.value[i].orderNumber == orderNumber.value) {
            
            multipleTableRef.value.toggleRowSelection(list.value[i], true)
          }
        }
      })
    } finally {
      loading.value = false
    }
  }
  const selectAll = () => {
    multipleTableRef.value.clearSelection()
  }
  const select = (selection, row) => {
    multipleTableRef.value.clearSelection()
    if (selection.length == 0) {
      isDelete.value = true
      return
    }
    multipleTableRef.value.toggleRowSelection(row, true)
    isDelete.value = false
  }
  /**选中 */
  const selectList = ref<any>([])
  const isDelete = ref(true)
  const handleSelectionChange = async (value) => {
    if (value.length == 1) {
      selectList.value = await value
    }
  }
</script>
<style scoped>
  .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #409eff;
    border-color: #409eff;
  }

  .el-checkbox__input .el-checkbox__inner::after {
    border-color: #fff;
  }
</style>
